package com.xianniu.ltc.config;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.DecimalFormat;

/**
 * @Author: dingyx
 * @Date: 2021/11/4 6:17 下午
 */
public class PercentageConvert implements Converter<Number> {
    @Override
    public Class supportJavaTypeKey() {
        return Number.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Number convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String cellString = cellData.getStringValue();
        String stringValue = cellString.replace("%", "");
        return NumberUtil.div(stringValue, "100");
    }

    @Override
    public CellData convertToExcelData(Number value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(new DecimalFormat("#.##%").format(value));
    }
}
